package com.xzavier.yunlian_interview.controller;

import com.xzavier.yunlian_interview.entity.QuestionItem;
import com.xzavier.yunlian_interview.entity.QuestionRecord;
import com.xzavier.yunlian_interview.service.QuestionRecordService;
import com.xzavier.yunlian_interview.service.ex.LoginException;
import com.xzavier.yunlian_interview.utils.JsonResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("interview/optRecord")
public class QuestionRecordController {
    @Autowired
    private QuestionRecordService questionRecordService;

    @GetMapping("readflag")
    public JsonResult<QuestionRecord> readFlag(HttpServletRequest request, @RequestParam String questionId) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        QuestionRecord questionRecord = questionRecordService.addQuestionRecord(userId, questionId);
        return new JsonResult<QuestionRecord>(200, questionRecord);
    }

    @PostMapping("liked")
    public JsonResult<QuestionRecord> OPTQuestionRecord(HttpServletRequest request, @RequestParam String questionId, @RequestParam Integer opt) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        questionRecordService.OptQuestionRecord(userId, questionId, opt);
        return new JsonResult<QuestionRecord>(200);
    }

    @PostMapping("collect")
    public JsonResult<QuestionRecord> CollectQuestionRecord(HttpServletRequest request, @RequestParam String questionId, @RequestParam Integer opt) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        questionRecordService.CollectQuestionRecord(userId, questionId, opt);
        return new JsonResult<QuestionRecord>(200);
    }

    @GetMapping("getreadflag")
    public JsonResult<List<QuestionItem>> getReadFlag(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        List<QuestionItem> readFlag = questionRecordService.getReadFlag(userId);
        return new JsonResult<>(200, readFlag);

    }

    @DeleteMapping("delete")
    public JsonResult<Void> delete(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        questionRecordService.deleteQuestionRecord(userId);
        return new JsonResult<>(200);
    }

    @DeleteMapping("delete/{id}")
    public JsonResult<Void> deleteQuestionRecord(HttpServletRequest request, @PathVariable("id") String questionId) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        questionRecordService.deleteQuestionRecord(userId, questionId);
        return new JsonResult<>(200);
    }

    @GetMapping("getliked")
    public JsonResult<List<QuestionItem>> getLiked(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        List<QuestionItem> likedQuestionRecord = questionRecordService.getLikedQuestionRecord(userId);
        return new JsonResult<>(200, likedQuestionRecord);
    }

    @GetMapping("getcollect")
    public JsonResult<List<QuestionItem>> getQuestionRecord(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            throw new LoginException("用户token不存在");
        }
        List<QuestionItem> likedQuestionRecord = questionRecordService.getCollectQuestionRecord(userId);
        return new JsonResult<>(200, likedQuestionRecord);
    }
}
